package com.songlanyun.modules.message.service.impl;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.songlanyun.common.enums.OpusConstant;
import com.songlanyun.common.enums.PrivateMessageConstant;
import com.songlanyun.common.enums.SysConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.BeanUtil;
import com.songlanyun.modules.account.service.UserInfoService;
import com.songlanyun.modules.exception.MemberException;
import com.songlanyun.modules.message.model.InteractionMessage;
import com.songlanyun.modules.message.model.to.PrivateMessageTO;
import com.songlanyun.modules.message.model.vo.PrivateMessageVO;
import com.songlanyun.modules.message.model.vo.UnreadMessageVO;
import com.songlanyun.modules.rabbitmq.MqConstant;
import com.songlanyun.modules.rabbitmq.service.SendMsgService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.message.dao.PrivateMessageDao;
import com.songlanyun.modules.message.entity.PrivateMessage;
import com.songlanyun.modules.message.service.PrivateMessageService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("privateMessageService")
public class PrivateMessageServiceImpl extends ServiceImpl<PrivateMessageDao, PrivateMessage> implements PrivateMessageService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Long userId = MapUtil.getLong(params, "userId");
        Integer type = MapUtil.getInt(params, "type");

        IPage<PrivateMessage> page = this.page(
                new Query<PrivateMessage>().getPage(params),
                new LambdaQueryWrapper<PrivateMessage>()
                        .eq(userId != null, PrivateMessage::getUserId, userId)
                        .eq(type != null, PrivateMessage::getType, type)
                        .orderByDesc(PrivateMessage::getCreateTime)
        );

        page.getRecords().forEach(PrivateMessage::loadMessageBody);

        return new PageUtils(page);
    }


    @Override
    public void saveMessage(PrivateMessageTO privateMessageTO) {
        PrivateMessage privateMessage = new PrivateMessage(privateMessageTO);

        //保存站内消息
        this.baseMapper.insert(privateMessage);
    }

    @Override
    public Integer unreadNum(Long userId) {
        return this.baseMapper.selectCount(
                new LambdaQueryWrapper<PrivateMessage>()
                        .eq(PrivateMessage::getIsRead, false)
                        .eq(PrivateMessage::getUserId, userId)
        );
    }

    @Override
    public List<UnreadMessageVO> unreadInfo(Long userId) {
        List<UnreadMessageVO> vos = new ArrayList<>();
        //查询各个消息类型的值
        for (PrivateMessageConstant.MessageType value : PrivateMessageConstant.MessageType.values()) {
            //查询类型和数量
            UnreadMessageVO vo = this.baseMapper.countByType(userId, value.getCode());
            if (vo.getType() == null) {
                vo.setType(value);
                vo.setNum(0);
            }

            //查询每个类型最新一条消息的时间和备注
            UnreadMessageVO newInfo = this.baseMapper.newInfo(userId, value.getCode());
            if (newInfo != null) {
                BeanUtil.copyPropertiesIgnoreNull(newInfo, vo);
            }

            vos.add(vo);
        }

        return vos;
    }

    @Override
    @Transactional
    public void readType(Integer type, Long userId) {
        this.update(
                new LambdaUpdateWrapper<PrivateMessage>()
                        .eq(PrivateMessage::getUserId, userId)
                        .eq(PrivateMessage::getIsRead, false)
                        .eq(PrivateMessage::getType, type)
                        .set(PrivateMessage::getIsRead, true)
        );
    }

    @Override
    public PrivateMessageVO info(Long id) {
        PrivateMessage privateMessage = this.getById(id, true);

        PrivateMessageVO vo = new PrivateMessageVO();
        BeanUtil.copyPropertiesIgnoreNull(privateMessage, vo);
        return vo;
    }

    @Override
    public PrivateMessage getById(Long id, Boolean isThrow) {
        PrivateMessage privateMessage = this.baseMapper.selectById(id);
        if (privateMessage == null && isThrow) {
            throw new RRException(MemberException.PRIVATE_MESSAGE_NOT_EXIST);
        }
        return privateMessage;
    }

    @Override
    public PageUtils listType(Map<String, Object> params) {
        return this.queryPage(params);
    }

}